<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class layout extends Admin_Controller {

	var $theme_data = array(
		'layout_main' => 'layout',
		'left' => 'include/leftcp',
	);
	var $resource = "";
	function __construct() {
		parent::__construct();
		$this->load->model("madmin");
		$this->url_index = admin_url() . "admin";
		$this->url_edit = $this->url_index . "/edit/";
		$this->url_delete = $this->url_index . "/delete/";
		$this->url_add = $this->url_index . "/edit/0";
		$this->load->model('mmodulesposition');
		$this->load->model('mmodules');
		
	}

	public function index($layout_name) {
		$data = array();
		$resCode =  'LAYOUT_'.strtoupper($layout_name);
		if (!$this->permission->check("insert",$resCode))
				return $this->nopermission();
		$this->resource = $this->mresource->get($resCode,'RES_CODE');
		
		$data['layout'] = $this->load->view('frontend/layout/' . $layout_name , array('is_admin' => true, ), true);
		$data['datas'] = array();
		$data['layout_name'] = $layout_name;
		$this->db->select('m.MOD_ID, m.MOD_NAME, m.MOD_TITLE, mp.*');
		$data['position'] =  $this->mmodulesposition->gets_widget('',$layout_name);
		$data['modules'] = $this->mmodules->gets();
		$this->theme_data["main"] = "layout/index";
		$this->template->viewAdmin($this->theme_data, $data);
	}
	
	public function update()
	{
		$layout_name = $this->input->post("layout");
		$module = $this->input->post("moduleid");
		if(!$layout_name) die();
		if(!$module || count($module) <=0) die();
		$arr_update = array();
		for($i=0; $i<count($module); $i++){
			$arr_update[] = array(
				'MPOS_ID'=>$module[$i],
				'MPOS_ID'=>$module[$i],
				'MPOS_ORDER'=>$i,
			);
		}

		$this->db->update_batch('modules_position', $arr_update, 'MPOS_ID'); 
	}
	
	public function load_widget_config($id) {
		$id = (int) $id;
		$mwidget = $this->mmodulesposition->get($id);
		
		if($mwidget) {
			$module = $this->mmodules->get($mwidget->MOD_ID);
			$mod_name = $module->MOD_NAME;
			$this->load->model('modules/'.$mod_name);
			//$form_content = $this->$mod_name->admin($widget);
			
			
			$widget = array(); 
			//$widget['widget_config'] =  $widget;
			$widget['widget_config'] = array();
			$widget['widget_config']['title'] = $mwidget->MPOS_TITLE;
			$widget['widget_config']['config'] = json_decode($mwidget->MPOS_CONFIG,true);
			$widget['widget_config']['layout'] = @$mwidget->MPOS_LAYOUT;
			$widget['mod_name']=  $mod_name;
			$widget['widget_id']=  $id;
			$widget['widget_title']=  $mwidget->MPOS_TITLE;
			
			$this->load->view('admin/layout/update_widget',$widget);
			 
		}else {
			die('Something wrong!');
		}
		
	}
	
	public function update_widget() {
		$data = $this->input->post();
		if (!$data ) 
			die("data not found");
		
		$module_id = $data['MPOS_ID'];
		$module = $this->mmodulesposition->get_module($module_id);
		if (!$module) 
			die("module not found");
		$module_name = $module->MOD_NAME;
		$this->load->model('modules/'.$module_name);
		$data = $this->$module_name->module_update($data);
		$arr_default = array('MPOS_TITLE'=>'','MPOS_ID'=>0);
		$data = array_merge($arr_default,$data);
		
		$title = $data['MPOS_TITLE'];
		$id = $module_id;
		unset($data['MPOS_ID']);
		unset($data['MPOS_TITLE']);
		$data_widget = array();
		$data_widget['MPOS_TITLE'] = $title;
		$config = json_encode($data);
		$data_widget['MPOS_CONFIG'] = $config;
		$this->mmodulesposition->update($module_id, 'MPOS_ID', $data_widget);
		
	}
	
	public function remove_widget() {
		if (!$this->input->is_ajax_request()) {
			die;
		}
		$wid = (int)$this->input->post('wid');
		if ($wid) {
			echo "delete $wid";
			$this->mmodulesposition->delete($wid);
		}
		die;
	}
	function add_widget(){
		if (!$this->input->is_ajax_request()) {
			die;
		}
		$module_id = $this->input->post("module_id");
		$position = $this->input->post("position");
		$layout = $this->input->post("layout");
		$module = $this->mmodules->get($module_id);
		if($module){
			$max_order = $this->db->select_max('MPOS_ORDER','max_order')
									->where('MPOS_LAYOUT',$layout)
									->where('MPOS_POSITION',$position)
									->get('modules_position')->first_row();
			$max_order = ($max_order->max_order >0)?$max_order->max_order:0;
			$arr_insert = array();
			$arr_insert['MPOS_POSITION'] = $position;
			$arr_insert['MPOS_LAYOUT'] = $layout;
			$arr_insert['MOD_ID'] =$module->MOD_ID;
			$arr_insert['MPOS_TITLE'] =$module->MOD_TITLE;
			$arr_insert['MPOS_CONFIG'] =$module->MOD_CONFIG;
			$arr_insert['MPOS_ORDER'] = $max_order+1;
			$mod_pos_id = $this->mmodulesposition->insert($arr_insert);
			die('<li data-id="'.$mod_pos_id.'" id="moduleid_'.$mod_pos_id.'"><div class="w-control" data-id="' . $mod_pos_id. '">'
			. '<a href="#editWidget"  data-toggle="modal"  class="w-edit glyphicon glyphicon-wrench"></a>'
			. '<a href="#AlertRemove" data-toggle="modal" class="w-remove glyphicon glyphicon-remove"></a></div><span>'.$module->MOD_TITLE.' : '.$arr_insert['MPOS_TITLE'].'</span></li>');
		}
		die("0");
	}
}
